
package edu.tfsu.team_2208.skylarkim.controller;


import edu.tfsu.team_2208.skylarkim.commons.DataResult;
import edu.tfsu.team_2208.skylarkim.commons.ResultData;
import edu.tfsu.team_2208.skylarkim.mapper.entity.Wordfeng;
import edu.tfsu.team_2208.skylarkim.service.HdfsService;
import edu.tfsu.team_2208.skylarkim.service.HdfsServiceFeng;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.java.Log;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Log
@RequestMapping("/hdfsFeng")
public class HdfsControllerFeng {

    @Resource
    private HdfsServiceFeng hdfsServiceFeng;

    private String demo;

    private String hdfs;


    @Operation(summary = "读取hadoop文件")
    @RequestMapping(value = "/readFeng",method = {RequestMethod.GET,RequestMethod.POST})
    public ResultData readHadoopFile(@RequestParam("pathFeng") String path, int start, int limit) {

        List<String> result = hdfsServiceFeng.readFile(path,start,limit);
        List<DataResult> jsonMap = new ArrayList<>();

        if (result != null) {
            for(String line : result){
                DataResult dr = new DataResult();
                dr.setName(line.split(",")[0]);
                dr.setValue(line.split(",")[1]);
                jsonMap.add(dr);

            }
            return ResultData.success(jsonMap);
        }

        return ResultData.error(500, "读取文件失败");
    }


    @RequestMapping(value = "/select",method = {RequestMethod.GET,RequestMethod.POST})
    public ResultData selectFromDB(int limit) {
        List<Wordfeng> result = hdfsServiceFeng.selectFromWordfeng(limit);
        if (result != null) {
            List<DataResult> jsonMap = new ArrayList<>();
            for (Wordfeng wordfeng : result) {
                DataResult dr = new DataResult();
                dr.setName(wordfeng.getWord());
                dr.setValue(wordfeng.getCounts().toString());
                jsonMap.add(dr);

            }
            return ResultData.success(result);
        }

        return ResultData.error(500,"读取数据库失败");

    }




}